package com.brainhealth.web.controller.customer;

import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.ValidationEvent;

import cn.hutool.Hutool;
import com.brainhealth.common.annotation.Anonymous;
import com.brainhealth.common.enums.ShowStatus;
import com.brainhealth.common.utils.DateCalcUtils;
import com.brainhealth.common.utils.DateUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.brainhealth.common.annotation.Log;
import com.brainhealth.common.core.controller.BaseController;
import com.brainhealth.common.core.domain.AjaxResult;
import com.brainhealth.common.enums.BusinessType;
import com.brainhealth.customer.domain.ShareLink;
import com.brainhealth.customer.service.IShareLinkService;
import com.brainhealth.common.utils.poi.ExcelUtil;
import com.brainhealth.common.core.page.TableDataInfo;

/**
 * 分享链接Controller
 * 
 * @author wolong
 * @date 2023-06-07
 */
@RestController
@RequestMapping("/customer/link")
public class ShareLinkController extends BaseController
{
    @Autowired
    private IShareLinkService shareLinkService;

    /**
     * 查询分享链接列表
     */
    @PreAuthorize("@ss.hasPermi('customer:link:list')")
    @GetMapping("/list")
    public TableDataInfo list(ShareLink shareLink)
    {
        startPage();
        List<ShareLink> list = shareLinkService.selectShareLinkList(shareLink);
        return getDataTable(list);
    }

    /**
     * 导出分享链接列表
     */
    @PreAuthorize("@ss.hasPermi('customer:link:export')")
    @Log(title = "分享链接", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ShareLink shareLink)
    {
        List<ShareLink> list = shareLinkService.selectShareLinkList(shareLink);
        ExcelUtil<ShareLink> util = new ExcelUtil<ShareLink>(ShareLink.class);
        util.exportExcel(response, list, "分享链接数据");
    }

    /**
     * 获取分享链接详细信息
     */
    @PreAuthorize("@ss.hasPermi('customer:link:query')")
    @GetMapping(value = "/{shareLinkId}")
    public AjaxResult getInfo(@PathVariable("shareLinkId") Long shareLinkId)
    {
        return success(shareLinkService.selectShareLinkByShareLinkId(shareLinkId));
    }

    /**
     * 新增分享链接
     */
    @PreAuthorize("@ss.hasPermi('customer:link:add')")
    @Log(title = "分享链接", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ShareLink shareLink)
    {
        return toAjax(shareLinkService.insertShareLink(shareLink));
    }

    /**
     * 修改分享链接
     */
    @PreAuthorize("@ss.hasPermi('customer:link:edit')")
    @Log(title = "分享链接", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ShareLink shareLink)
    {
        return toAjax(shareLinkService.updateShareLink(shareLink));
    }

    /**
     * 删除分享链接
     */
    @PreAuthorize("@ss.hasPermi('customer:link:remove')")
    @Log(title = "分享链接", businessType = BusinessType.DELETE)
	@DeleteMapping("/{shareLinkIds}")
    public AjaxResult remove(@PathVariable Long[] shareLinkIds)
    {
        return toAjax(shareLinkService.deleteShareLinkByShareLinkIds(shareLinkIds));
    }

    /**
     * 分享连接ID获取分享连接信息
     * @param map
     * @return
     */
    @Anonymous
    @PostMapping("/getShareLinkById")
    public AjaxResult getShareLinkById(@RequestBody Map<String, Object> map)
    {
        Long shareLinkId = Long.valueOf(map.get("shareLinkId").toString());
        ShareLink shareLink = shareLinkService.selectShareLinkByShareLinkId(shareLinkId);
        Date vaildDate = DateCalcUtils.addDay(shareLink.getCreateTime(), shareLink.getValidDay().intValue());
        Long time = vaildDate.getTime() - new Date().getTime();
        if (time < 0)
        {
            return AjaxResult.error("连接已经失效");
        }
        if (shareLink.getStatus()== ShowStatus.DownShelves)
        {
            return AjaxResult.error("连接已经下架");
        }
        return AjaxResult.success(shareLink);
    }
}
